﻿//****************************************************************************
// Copyright (c) 2010 All Right Reserved, http://www.sublight.si/
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from
// the use of this software.
//
// The origin of this software must not be misrepresented; you must not claim
// that you wrote the original software. If you use this software in a product,
// an acknowledgment in the product documentation is required.
//
// Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// The source code of the program may be copied and used for educational,
// non-commercial purposes. The source code may be modified if desired,
// to suit the user.
//
// This notice may not be removed or altered from any source distribution.

using System;
using NConsoler;

namespace SublightCmd
{
    /// <summary>
    /// SublightCmd help download
    /// Download Slovenian and English subtitles:
    /// SublightCmd download "C:\video.avi" "sl"+"en"
    /// Download max rated Slovenian and English subtitles for video.avi, write them to "C:\" directory:
    /// SublightCmd download "C:\video.avi" "sl"+"en" \priority:MaxRate \output:"C:\"
    /// </summary>
    partial class Program
    {
        [Action("automatically download subtitle(s)")]
        public static void Download(
            [Required(Description = "Video file")]
            string video,
            [Required(Description = "Subtitle languages")]
            string[] languages,
            [Optional("", Description="MaxDownloads|MaxRate|MinReports|Newest")]
            string priority,
            [Optional("", Description = "Output location")]
            string output,
            [Optional("", Description = "Sublight username for login")]
            string username
            )
        {
            Guid session = Guid.Empty;

            try
            {
                if (!string.IsNullOrEmpty(username))
                {
                    Console.Write(string.Format("Please enter password for Sublight username '{0}': ", username));
                    ConsoleColor foreColor = Console.ForegroundColor;
                    Console.ForegroundColor = Console.BackgroundColor;
                    string password = Console.ReadLine();
                    Console.ForegroundColor = foreColor;
                    session = Sublight.Lib.Session.Util.LogIn(username, password);
                }
                else
                {
                    throw new Exception("Missing parameter: username");
                }

                Sublight.Lib.Subtitle.Util.DownloadPausedHandler downloadPausedHandler =
                delegate(string message, int seconds)
                {
                    Console.WriteLine(message);
                };

                string[] res = Sublight.Lib.Subtitle.Util.DownloadByHash(session, video, languages, priority, output, downloadPausedHandler);
                if (res.Length <= 0)
                {
                    Console.WriteLine("Could not automatically find any subtitles. Please run Sublight and use manual search.");
                }
                else
                {
                    Console.WriteLine("Downloaded subtitles:");
                    foreach (string subtitle in res)
                    {
                        Console.WriteLine(subtitle);
                    }
                }
            }
            catch (Exception ex)
            {
                Environment.ExitCode = 1;
                Console.Error.WriteLine(string.Format("Error occured: {0}", ex.Message));
            }
            finally
            {
                if (session != Guid.Empty)
                {
                    try
                    {
                        Sublight.Lib.Session.Util.LogOut(session);
                    }
                    catch
                    { 
                    }
                }
            }
        }
    }
}
